package com.example.busquerysystem.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.SupportMapFragment;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.search.busline.BusLineSearch;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRoutePlanOption;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteLine;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.example.busquerysystem.R;
import com.example.busquerysystem.activity.TransferschemeActivity;
import com.example.busquerysystem.view.TransitRouteOverlay;

public class TransferFragment extends Fragment {

    private BaiduMap bdmap;
    private TextureMapView mapView;
    private static RoutePlanSearch mSearch;
    private TransitRouteLine route;

    private EditText stEdit;
    private EditText edEdit;

    private Button chaxun;
    private Button alldatas;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View view = layoutInflater.inflate(R.layout.activity_transfer, viewGroup, false);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        SDKInitializer.initialize(getActivity().getApplicationContext());
        initView();
    }

    public void initView() {
        mapView = getView().findViewById(R.id.amapview);
        bdmap = mapView.getMap();

        stEdit = getView().findViewById(R.id.edit_transfer_from);
        edEdit = getView().findViewById(R.id.edit_transfer_to);
        chaxun = getView().findViewById(R.id.select_transfer_button);
        alldatas = getView().findViewById(R.id.next_button);

        mSearch = RoutePlanSearch.newInstance();
        mSearch.setOnGetRoutePlanResultListener(routePlanResultListener);

        chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //换乘查询
                transferSearch();
//
            }
        });
        alldatas.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //查询详情
                chakanSearch();
            }
        });

    }

    private void transferSearch() {
        String city = "株洲";
//        Toast.makeText(getActivity(), city+"!!!", Toast.LENGTH_SHORT).show();
        PlanNode stMassNode = PlanNode.withCityNameAndPlaceName(city, stEdit.getText().toString().trim());
        PlanNode enMassNode = PlanNode.withCityNameAndPlaceName(city, edEdit.getText().toString().trim());
        mSearch.transitSearch(new TransitRoutePlanOption().from(stMassNode).to(enMassNode).city(city.toString()));
//        Log.e("----------->>>>",route.getDistance()+"");
    }

    private void chakanSearch() {
        startActivity(new Intent(getActivity(), TransferschemeActivity.class));
    }


    OnGetRoutePlanResultListener routePlanResultListener = new OnGetRoutePlanResultListener() {
        @Override
        public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {

        }

        @Override
        public void onGetTransitRouteResult(TransitRouteResult result) {
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                //未找到结果
                Log.e("-------->", result.error + "");
                Log.e("-------->", SearchResult.ERRORNO.NO_ERROR + "");
                Toast.makeText(getActivity(), "未找到结果", Toast.LENGTH_SHORT).show();
                return;
            }
            if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
                //起终点或途经点地址有岐义，通过以下接口获取建议查询信息
//                result.getSuggestAddrInfo()
                return;
            }
            if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                bdmap.clear();
                route = result.getRouteLines().get(0);
                //创建公交路线规划线路覆盖物
                TransitRouteOverlay overlay = new TransitRouteOverlay(bdmap);
                //设置公交路线规划数据
                overlay.setData(route);
                //将公交路线规划覆盖物添加到地图中
                overlay.addToMap();
                overlay.zoomToSpan();
            }


        }

        @Override
        public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

        }

        @Override
        public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {

        }

        @Override
        public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

        }

        @Override
        public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

        }
    };


    @Override
    public void onDestroy() {
        super.onDestroy();
        mSearch.destroy();
    }
}